home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / VideoFolder 1.0a / Source / MoreFiles 1.4.1 / MoreFilesExtras.c < prev    next >
Text File  |  1996-01-06  |  74KB  |  2,509 lines

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    A collection of useful high-level File Manager routines.
  5. **
  6. **    by Jim Luther, Apple Developer Technical Support Emeritus
  7. **
  8. **    File:        MoreFilesExtras.c
  9. **
  10. **    Copyright © 1992-1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include <Types.h>
  23. #include <Errors.h>
  24. #include <Files.h>
  25. #include <Devices.h>
  26. #include <Folders.h>
  27. #include <FSM.h>
  28. #include <Disks.h>
  29. #include <Gestalt.h>
  30. #include "MoreFiles.h"
  31. #include "MoreFilesExtras.h"
  32.  
  33. /*****************************************************************************/
  34.  
  35. /* local data structures */
  36.  
  37. /* The DeleteEnumGlobals structure is used to minimize the amount of
  38. ** stack space used when recursively calling DeleteLevel and to hold
  39. ** global information that might be needed at any time. */
  40.  
  41. #if PRAGMA_ALIGN_SUPPORTED
  42. #pragma options align=mac68k
  43. #endif
  44. struct DeleteEnumGlobals
  45. {
  46.     OSErr            error;                /* temporary holder of results - saves 2 bytes of stack each level */
  47.     Str63            itemName;            /* the name of the current item */
  48.     UniversalFMPB    myPB;                /* the parameter block used for PBGetCatInfo calls */
  49. };
  50. #if PRAGMA_ALIGN_SUPPORTED
  51. #pragma options align=reset
  52. #endif
  53.  
  54. typedef struct DeleteEnumGlobals DeleteEnumGlobals;
  55. typedef DeleteEnumGlobals *DeleteEnumGlobalsPtr;
  56.  
  57. /*****************************************************************************/
  58.  
  59. pascal    Ptr    GetTempBuffer(long buffReqSize,
  60.                           long *buffActSize)
  61. {
  62.     enum
  63.     {
  64.         kSlopMemory = 0x00008000    /* 32K - Amount of free memory to leave when allocating buffers */
  65.     };
  66.     Ptr    tempPtr;
  67.     
  68.     /* Make request a multiple of 1024 bytes */
  69.     buffReqSize = buffReqSize & 0xfffffc00;
  70.     
  71.     if ( buffReqSize < 0x00000400 )
  72.     {
  73.         /* Request was smaller than 1024 bytes - make it 1024 */
  74.         buffReqSize = 0x00000400;
  75.     }
  76.     
  77.     /* Attempt to allocate the memory */
  78.     tempPtr = NewPtr(buffReqSize);
  79.     
  80.     /* If request failed, go to backup plan */
  81.     if ( (tempPtr == NULL) && (buffReqSize > 0x00000400) )
  82.     {
  83.         /*
  84.         **    Try to get largest 1024-byte block available
  85.         **    leaving some slop for the toolbox if possible
  86.         */
  87.         long freeMemory = (FreeMem() - kSlopMemory) & 0xfffffc00;
  88.         
  89.         buffReqSize = MaxBlock() & 0xfffffc00;
  90.         
  91.         if ( buffReqSize > freeMemory )
  92.         {
  93.             buffReqSize = freeMemory;
  94.         }
  95.         
  96.         if ( buffReqSize == 0 )
  97.         {
  98.             buffReqSize = 0x00000400;
  99.         }
  100.         
  101.         tempPtr = NewPtr(buffReqSize);
  102.     }
  103.     
  104.     /* Return bytes allocated */
  105.     if ( tempPtr != NULL )
  106.     {
  107.         *buffActSize = buffReqSize;
  108.     }
  109.     else
  110.     {
  111.         *buffActSize = 0;
  112.     }
  113.     
  114.     return ( tempPtr );
  115. }
  116.  
  117. /*****************************************************************************/
  118.  
  119. pascal    OSErr    DetermineVRefNum(StringPtr pathname,
  120.                                  short vRefNum,
  121.                                  short *realVRefNum)
  122. {
  123.     HParamBlockRec pb;
  124.     Str255 tempPathname;
  125.     OSErr error;
  126.  
  127.     pb.volumeParam.ioVRefNum = vRefNum;
  128.     if ( pathname == NULL )
  129.     {
  130.         pb.volumeParam.ioNamePtr = NULL;
  131.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  132.     }
  133.     else
  134.     {
  135.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  136.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  137.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  138.     }
  139.     error = PBHGetVInfoSync(&pb);
  140.     *realVRefNum = pb.volumeParam.ioVRefNum;
  141.     return ( error );
  142. }
  143.  
  144. /*****************************************************************************/
  145.  
  146. pascal    OSErr    HGetVInfo(short volReference,
  147.                           StringPtr volName,
  148.                           short *vRefNum,
  149.                           unsigned long *freeBytes,
  150.                           unsigned long *totalBytes)
  151. {
  152.     HParamBlockRec    pb;
  153.     unsigned long    allocationBlockSize;
  154.     unsigned short    numAllocationBlocks;
  155.     unsigned short    numFreeBlocks;
  156.     VCB                *theVCB;
  157.     Boolean            vcbFound;
  158.     OSErr            result;
  159.     
  160.     /* Use the File Manager to get the real vRefNum */
  161.     pb.volumeParam.ioVRefNum = volReference;
  162.     pb.volumeParam.ioNamePtr = volName;
  163.     pb.volumeParam.ioVolIndex = 0;    /* use ioVRefNum only, return volume name */
  164.     result = PBHGetVInfoSync(&pb);
  165.     
  166.     if ( result == noErr )
  167.     {
  168.         /* The volume name was returned in volName (if not NULL) and */
  169.         /* we have the volume's vRefNum and allocation block size */
  170.         *vRefNum = pb.volumeParam.ioVRefNum;
  171.         allocationBlockSize = (unsigned long)pb.volumeParam.ioVAlBlkSiz;
  172.         
  173.         /* System 7.5 (and beyond) pins the number of allocation blocks and */
  174.         /* the number of free allocation blocks returned by PBHGetVInfo to */
  175.         /* a value so that when multiplied by the allocation block size, */
  176.         /* the volume will look like it has $7fffffff bytes or less. This */
  177.         /* was done so older applications that use signed math or that use */
  178.         /* the GetVInfo function (which uses signed math) will continue to work. */
  179.         /* However, the unpinned numbers (which we want) are always available */
  180.         /* in the volume's VCB so we'll get those values from the VCB if possible. */
  181.         
  182.         /* Find the volume's VCB */
  183.         vcbFound = false;
  184.         theVCB = (VCB *)(GetVCBQHdr()->qHead);
  185.         while ( (theVCB != NULL) && !vcbFound )
  186.         {
  187.             /* Check VCB signature before using VCB. Don't have to check for */
  188.             /* MFS (0xd2d7) because they can't get big enough to be pinned */
  189.             if ( theVCB->vcbSigWord == 0x4244 )
  190.             {
  191.                 if ( theVCB->vcbVRefNum == *vRefNum )
  192.                 {
  193.                     vcbFound = true;
  194.                 }
  195.             }
  196.             
  197.             if ( !vcbFound )
  198.             {
  199.                 theVCB = (VCB *)(theVCB->qLink);
  200.             }
  201.         }
  202.         
  203.         if ( theVCB != NULL )
  204.         {
  205.             /* Found a VCB we can use. Get the un-pinned number of allocation blocks */
  206.             /* and the number of free blocks from the VCB. */
  207.             numAllocationBlocks = (unsigned short)theVCB->vcbNmAlBlks;
  208.             numFreeBlocks = (unsigned short)theVCB->vcbFreeBks;
  209.         }
  210.         else
  211.         {
  212.             /* Didn't find a VCB we can use. Return the number of allocation blocks */
  213.             /* and the number of free blocks returned by PBHGetVInfoSync. */
  214.             numAllocationBlocks = (unsigned short)pb.volumeParam.ioVNmAlBlks;
  215.             numFreeBlocks = (unsigned short)pb.volumeParam.ioVFrBlk;
  216.         }
  217.         
  218.         /* Now, calculate freeBytes and totalBytes using unsigned values */
  219.         *freeBytes = numFreeBlocks * allocationBlockSize;
  220.         *totalBytes = numAllocationBlocks * allocationBlockSize;
  221.     }
  222.     
  223.     return ( result );
  224. }
  225.  
  226. /*****************************************************************************/
  227.  
  228. pascal    OSErr    XGetVInfo(short volReference,
  229.                           StringPtr volName,
  230.                           short *vRefNum,
  231.                           UnsignedWide *freeBytes,
  232.                           UnsignedWide *totalBytes)
  233. {
  234.     OSErr            result;
  235.     long            response;
  236.     XVolumeParam    pb;
  237.     
  238.     /* See if large volume support is available */
  239.     if ( ( Gestalt(gestaltFSAttr, &response) == noErr ) && ((response & (1L << gestaltFSSupports2TBVols)) != 0) )
  240.     {
  241.         /* Large volume support is available */
  242.         pb.ioVRefNum = volReference;
  243.         pb.ioNamePtr = volName;
  244.         pb.ioXVersion = 0;    /* this XVolumeParam version (0) */
  245.         pb.ioVolIndex = 0;    /* use ioVRefNum only, return volume name */
  246.         result = PBXGetVolInfoSync(&pb);
  247.         if ( result == noErr )
  248.         {
  249.             /* The volume name was returned in volName (if not NULL) and */
  250.             /* we have the volume's vRefNum and allocation block size */
  251.             *vRefNum = pb.ioVRefNum;
  252.             
  253.             /* return the freeBytes and totalBytes */
  254.             *totalBytes = pb.ioVTotalBytes;
  255.             *freeBytes = pb.ioVFreeBytes;
  256.         }
  257.     }
  258.     else
  259.     {
  260.         /* No large volume support */
  261.         
  262.         /* zero the high longs of totalBytes and freeBytes */
  263.         totalBytes->hi = 0;
  264.         freeBytes->hi = 0;
  265.         
  266.         /* Use HGetVInfo to get the results */
  267.         result = HGetVInfo(volReference, volName, vRefNum, &freeBytes->lo, &totalBytes->lo);
  268.     }
  269.     return ( result );
  270. }
  271.  
  272. /*****************************************************************************/
  273.  
  274. pascal    OSErr    CheckVolLock(StringPtr pathname,
  275.                              short vRefNum)
  276. {
  277.     HParamBlockRec pb;
  278.     Str255 tempPathname;
  279.     OSErr error;
  280.  
  281.     pb.volumeParam.ioVRefNum = vRefNum;
  282.     if ( pathname == NULL )
  283.     {
  284.         pb.volumeParam.ioNamePtr = NULL;
  285.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  286.     }
  287.     else
  288.     {
  289.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  290.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  291.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  292.     }
  293.     error = PBHGetVInfoSync(&pb);
  294.     
  295.     if ( error == noErr )
  296.     {
  297.         if ( (pb.volumeParam.ioVAtrb & 0x0080) != 0 )
  298.             error = wPrErr;        /* volume locked by hardware */
  299.         else if ( (pb.volumeParam.ioVAtrb & 0x8000) != 0 )
  300.             error = vLckdErr;    /* volume locked by software */
  301.     }
  302.     
  303.     return ( error );
  304. }
  305.  
  306. /*****************************************************************************/
  307.  
  308. pascal    OSErr GetDriverName(short driverRefNum,
  309.                             Str255 driverName)
  310. {
  311.     OSErr result;
  312.     DCtlHandle theDctl;
  313.     DRVRHeaderPtr dHeaderPtr;
  314.     
  315.     theDctl = GetDCtlEntry(driverRefNum);
  316.     if ( theDctl != NULL )
  317.     {
  318.         if ( (**theDctl).dCtlFlags & 0x40 )
  319.         {
  320.             /* dctlDriver is handle - dereference */
  321.             dHeaderPtr = (DRVRHeaderPtr)*((**theDctl).dCtlDriver);
  322.         }
  323.         else
  324.         {
  325.             /* dctlDriver is pointer */
  326.           dHeaderPtr = (DRVRHeaderPtr)(**theDctl).dCtlDriver;
  327.         }
  328.         BlockMoveData((*dHeaderPtr).drvrName, driverName, (*dHeaderPtr).drvrName[0] + 1);
  329.         result = noErr;
  330.     }
  331.     else
  332.     {
  333.         driverName[0] = 0;
  334.         result = badUnitErr;    /* bad reference number */
  335.     }
  336.     
  337.     return ( result );
  338. }
  339.  
  340. /*****************************************************************************/
  341.  
  342. pascal    OSErr    FindDrive(StringPtr pathname,
  343.                           short vRefNum,
  344.                           DrvQElPtr *driveQElementPtr)
  345. {
  346.     OSErr            result;
  347.     Str255            tempPathname;
  348.     HParamBlockRec    hPB;
  349.     short            driveNumber;
  350.     
  351.     *driveQElementPtr = NULL;
  352.     
  353.     /* First, use PBHGetVInfo to determine the volume */
  354.     hPB.volumeParam.ioVRefNum = vRefNum;
  355.     if ( pathname == NULL )
  356.     {
  357.         hPB.volumeParam.ioNamePtr = NULL;
  358.         hPB.volumeParam.ioVolIndex = 0;    /* use ioVRefNum only */
  359.     }
  360.     else
  361.     {
  362.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  363.         hPB.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  364.         hPB.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  365.     }
  366.     result = PBHGetVInfoSync(&hPB);
  367.     if ( result == noErr )
  368.     {
  369.         /*
  370.         **    The volume can be either online, offline, or ejected. What we find in
  371.         **    ioVDrvInfo and ioVDRefNum will tell us which it is.
  372.         **    See Inside Macintosh: Files page 2-80 and the Technical Note
  373.         **    "FL 34 - VCBs and Drive Numbers : The Real Story"
  374.         **    Where we get the drive number depends on the state of the volume.
  375.         */
  376.         if ( hPB.volumeParam.ioVDrvInfo != 0 )
  377.         {
  378.             /* The volume is online and not ejected */
  379.             /* Get the drive number */
  380.             driveNumber = hPB.volumeParam.ioVDrvInfo;
  381.         }
  382.         else
  383.         {
  384.             /* The volume's is either offline or ejected */
  385.             /* in either case, the volume is NOT online */
  386.  
  387.             /* Is it ejected or just offline? */
  388.             if ( hPB.volumeParam.ioVDRefNum > 0 )
  389.             {
  390.                 /* It's ejected, the drive number is ioVDRefNum */
  391.                 driveNumber = hPB.volumeParam.ioVDRefNum;
  392.             }
  393.             else
  394.             {
  395.                 /* It's offline, the drive number is the negative of ioVDRefNum */
  396.                 driveNumber = (short)-hPB.volumeParam.ioVDRefNum;
  397.             }
  398.         }
  399.         
  400.         /* Get pointer to first element in drive queue */
  401.         *driveQElementPtr = (DrvQElPtr)(GetDrvQHdr()->qHead);
  402.         
  403.         /* Search for a matching drive number */
  404.         while ( (*driveQElementPtr != NULL) && ((*driveQElementPtr)->dQDrive != driveNumber) )
  405.         {
  406.             *driveQElementPtr = (DrvQElPtr)(*driveQElementPtr)->qLink;
  407.         }
  408.         
  409.         if ( *driveQElementPtr == NULL )
  410.         {
  411.             /* This should never happen since every volume must have a drive, but... */
  412.             result = nsDrvErr;
  413.         }
  414.     }
  415.     
  416.     return ( result );
  417. }
  418.  
  419. /*****************************************************************************/
  420.  
  421. pascal    OSErr    GetDiskBlocks(StringPtr pathname,
  422.                               short vRefNum,
  423.                               unsigned long *numBlocks)
  424. {
  425.     /* Various constants for GetDiskBlocks() */
  426.     enum
  427.     {
  428.         /* return format list status code */
  429.         kFmtLstCode = 6,
  430.         
  431.         /* reference number of .SONY driver */
  432.         kSonyRefNum = 0xfffb,
  433.         
  434.         /* values returned by DriveStatus in DrvSts.twoSideFmt */
  435.         kSingleSided = 0,
  436.         kDoubleSided = -1,
  437.         kSingleSidedSize = 800,        /* 400K */
  438.         kDoubleSidedSize = 1600,    /* 800K */
  439.         
  440.         /* values in DrvQEl.qType */
  441.         kWordDrvSiz = 0,
  442.         kLongDrvSiz = 1,
  443.         
  444.         /* more than enough formatListRecords */
  445.         kMaxFormatListRecs = 16
  446.     };
  447.     
  448.     DrvQElPtr        driveQElementPtr;
  449.     unsigned long    blocks;
  450.     ParamBlockRec    pb;
  451.     FormatListRec    formatListRecords[kMaxFormatListRecs];
  452.     DrvSts            status;
  453.     short            formatListRecIndex;
  454.     OSErr            result;
  455.  
  456.     blocks = 0;
  457.     
  458.     /* Find the drive queue element for this volume */
  459.     result = FindDrive(pathname, vRefNum, &driveQElementPtr);
  460.     
  461.     /* 
  462.     **    Make sure this is a real driver (dQRefNum < 0).
  463.     **    AOCE's Mail Enclosures volume uses 0 for dQRefNum which will cause
  464.     **    problems if you try to use it as a driver refNum.
  465.     */ 
  466.     if ( driveQElementPtr->dQRefNum >= 0 )
  467.         result = paramErr;
  468.         
  469.     if ( result == noErr )
  470.     {
  471.         /* Attempt to get the drive's format list. */
  472.         /* (see the Technical Note "What Your Sony Drives For You") */
  473.         
  474.         pb.cntrlParam.ioVRefNum = driveQElementPtr->dQDrive;
  475.         pb.cntrlParam.ioCRefNum = driveQElementPtr->dQRefNum;
  476.         pb.cntrlParam.csCode = kFmtLstCode;
  477.         pb.cntrlParam.csParam[0] = kMaxFormatListRecs;
  478.         *(long *)&pb.cntrlParam.csParam[1] = (long)&formatListRecords[0];
  479.         
  480.         result = PBStatusSync(&pb);
  481.         
  482.         if ( result == noErr )
  483.         {
  484.             /* The drive supports ReturnFormatList status call. */
  485.             
  486.             /* Get the current disk's size. */
  487.             for( formatListRecIndex = 0;
  488.                  formatListRecIndex < pb.cntrlParam.csParam[0];
  489.                  ++formatListRecIndex )
  490.             {
  491.                 if ( (formatListRecords[formatListRecIndex].formatFlags &
  492.                       diCIFmtFlagsCurrentMask) != 0 )
  493.                 {
  494.                     blocks = formatListRecords[formatListRecIndex].volSize;
  495.                 }
  496.             }
  497.             if ( blocks == 0 )
  498.             {
  499.                 /* This should never happen */
  500.                 result = paramErr;
  501.             }
  502.         }
  503.         else if ( driveQElementPtr->dQRefNum == (short)kSonyRefNum )
  504.         {
  505.             /* The drive is a non-SuperDrive floppy which only supports 400K and 800K disks */
  506.             
  507.             result = DriveStatus(driveQElementPtr->dQDrive, &status);
  508.             if ( result == noErr )
  509.             {
  510.                 switch ( status.twoSideFmt )
  511.                 {
  512.                 case kSingleSided:
  513.                     blocks = kSingleSidedSize;
  514.                     break;
  515.                 case kDoubleSided:
  516.                     blocks = kDoubleSidedSize;
  517.                     break;
  518.                 default:
  519.                     /* This should never happen */
  520.                     result = paramErr;
  521.                     break;
  522.                 }
  523.             }
  524.         }
  525.         else
  526.         {
  527.             /* The drive is not a floppy and it doesn't support ReturnFormatList */
  528.             /* so use the dQDrvSz field(s) */
  529.             
  530.             result = noErr;    /* reset result */
  531.             switch ( driveQElementPtr->qType )
  532.             {
  533.             case kWordDrvSiz:
  534.                 blocks = driveQElementPtr->dQDrvSz;
  535.                 break;
  536.             case kLongDrvSiz:
  537.                 blocks = ((unsigned long)driveQElementPtr->dQDrvSz2 << 16) +
  538.                          driveQElementPtr->dQDrvSz;
  539.                 break;
  540.             default:
  541.                 /* This should never happen */
  542.                 result = paramErr;
  543.                 break;
  544.             }
  545.         }
  546.     }
  547.     
  548.     *numBlocks = blocks;
  549.     return ( result );
  550. }
  551.  
  552. /*****************************************************************************/
  553.  
  554. pascal    OSErr    UnmountAndEject(StringPtr pathname,
  555.                                 short vRefNum)
  556. {
  557.     HParamBlockRec pb;
  558.     Str255 tempPathname;
  559.     short driveNum;
  560.     Boolean ejected, wantsEject;
  561.     DrvQElPtr drvQElem;
  562.     OSErr error;
  563.  
  564.     pb.volumeParam.ioVRefNum = vRefNum;
  565.     if ( pathname == NULL )
  566.     {
  567.         pb.volumeParam.ioNamePtr = NULL;
  568.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  569.     }
  570.     else
  571.     {
  572.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  573.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  574.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  575.     }
  576.     error = PBHGetVInfoSync(&pb);
  577.     if ( error == noErr )
  578.     {
  579.         if ( pb.volumeParam.ioVDrvInfo != 0 )
  580.         {
  581.             /* the volume is online and not ejected */
  582.             ejected = false;
  583.             
  584.             /* Get the drive number */
  585.             driveNum = pb.volumeParam.ioVDrvInfo;
  586.         }
  587.         else
  588.         {
  589.             /* the volume is ejected or offline */
  590.             
  591.             /* Is it ejected? */
  592.             ejected = pb.volumeParam.ioVDRefNum > 0;
  593.             
  594.             if ( ejected )
  595.             {
  596.                 /* If ejected, the drive number is ioVDRefNum */
  597.                 driveNum = pb.volumeParam.ioVDRefNum;
  598.             }
  599.             else
  600.             {
  601.                 /* If offline, the drive number is the negative of ioVDRefNum */
  602.                 driveNum = (short)-pb.volumeParam.ioVDRefNum;
  603.             }
  604.         }
  605.         
  606.         /* find the drive queue element */
  607.         drvQElem = (DrvQElPtr)(GetDrvQHdr()->qHead);
  608.         while ( (drvQElem != NULL) && (drvQElem->dQDrive != driveNum) )
  609.         {
  610.             drvQElem = (DrvQElPtr)drvQElem->qLink;
  611.         }
  612.         
  613.         if ( drvQElem != NULL )
  614.         {
  615.             /* does the drive want an eject call */
  616.             wantsEject = (*((Ptr)((Ptr)drvQElem - 3)) != 8);
  617.         }
  618.         else
  619.         {
  620.             /* didn't find the drive!! */
  621.             wantsEject = false;
  622.         }
  623.         
  624.         /* unmount the volume */
  625.         pb.volumeParam.ioNamePtr = NULL;
  626.         /* ioVRefNum is already filled in from PBHGetVInfo */
  627.         error = PBUnmountVol((ParmBlkPtr)&pb);
  628.         if ( error == noErr )
  629.         {
  630.             if ( wantsEject && !ejected )
  631.             {
  632.                 /* eject the media from the drive if needed */
  633.                 pb.volumeParam.ioVRefNum = driveNum;
  634.                 error = PBEject((ParmBlkPtr)&pb);
  635.             }
  636.         }
  637.     }
  638.     return ( error );
  639. }
  640.  
  641. /*****************************************************************************/
  642.  
  643. pascal    OSErr    OnLine(FSSpecPtr volumes,
  644.                        short reqVolCount,
  645.                        short *actVolCount,
  646.                        short *volIndex)
  647. {
  648.     HParamBlockRec pb;
  649.     OSErr error = noErr;
  650.     FSSpec *endVolArray = volumes + reqVolCount;
  651.  
  652.     *actVolCount = 0;
  653.     for ( ; (volumes < endVolArray) && (error == noErr); ++volumes )
  654.     {
  655.         pb.volumeParam.ioNamePtr = (StringPtr) & volumes->name;
  656.         pb.volumeParam.ioVolIndex = *volIndex;
  657.         error = PBHGetVInfoSync(&pb);
  658.         if ( error == noErr )
  659.         {
  660.             volumes->parID = fsRtParID;        /* the root directory's parent is 1 */
  661.             volumes->vRefNum = pb.volumeParam.ioVRefNum;
  662.             ++*volIndex;
  663.             ++*actVolCount;
  664.         }
  665.     }
  666.     return ( error );
  667. }
  668.  
  669. /*****************************************************************************/
  670.  
  671. pascal    OSErr SetDefault(short newVRefNum,
  672.                          long newDirID,
  673.                          short *oldVRefNum,
  674.                          long *oldDirID)
  675. {
  676.     OSErr    error;
  677.     
  678.     /* Get the current default volume/directory. */
  679.     error = HGetVol(NULL, oldVRefNum, oldDirID);
  680.     
  681.     if ( error == noErr )
  682.         /* Set the new default volume/directory */
  683.         error = HSetVol(NULL, newVRefNum, newDirID);
  684.     return ( error );
  685. }
  686.  
  687. /*****************************************************************************/
  688.  
  689. pascal    OSErr RestoreDefault(short oldVRefNum,
  690.                              long oldDirID)
  691. {
  692.     OSErr    error;
  693.     short    defaultVRefNum;
  694.     long    defaultDirID;
  695.     long    defaultProcID;
  696.     
  697.     /* Determine if the default volume was a wdRefNum. */
  698.     error = GetWDInfo(oldVRefNum, &defaultVRefNum, &defaultDirID, &defaultProcID);
  699.     
  700.     if ( error == noErr )
  701.     {
  702.         /* Restore the old default volume/directory, one way or the other. */
  703.         if ( defaultDirID != fsRtDirID )
  704.             /* oldVRefNum was a wdRefNum - use SetVol */
  705.             error = SetVol(NULL, oldVRefNum);
  706.         else
  707.             /* oldVRefNum was a real vRefNum - use HSetVol */
  708.             error = HSetVol(NULL, oldVRefNum, oldDirID);
  709.     }
  710.     
  711.     return ( error );
  712. }
  713.  
  714. /*****************************************************************************/
  715.  
  716. pascal    OSErr GetDInfo(short vRefNum,
  717.                        long dirID,
  718.                        StringPtr name,
  719.                        DInfo *fndrInfo)
  720. {
  721.     CInfoPBRec pb;
  722.     Str31 tempName;
  723.     OSErr error;
  724.     
  725.     /* Protection against File Sharing problem */
  726.     if ( (name == NULL) || (name[0] == 0) )
  727.     {
  728.         tempName[0] = 0;
  729.         pb.dirInfo.ioNamePtr = tempName;
  730.         pb.dirInfo.ioFDirIndex = -1;    /* use ioDirID */
  731.     }
  732.     else
  733.     {
  734.         pb.dirInfo.ioNamePtr = name;
  735.         pb.dirInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  736.     }
  737.     pb.dirInfo.ioVRefNum = vRefNum;
  738.     pb.dirInfo.ioDrDirID = dirID;
  739.     error = PBGetCatInfoSync(&pb);
  740.     if ( error == noErr )
  741.     {
  742.         if ( (pb.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  743.             /* it's a directory, return the DInfo */
  744.             *fndrInfo = pb.dirInfo.ioDrUsrWds;
  745.         else
  746.             /* oops, a file was passed */
  747.             error = dirNFErr;
  748.     }
  749.     return ( error );
  750. }
  751.  
  752. /*****************************************************************************/
  753.  
  754. pascal    OSErr FSpGetDInfo(const FSSpec *spec,
  755.                           DInfo *fndrInfo)
  756. {
  757.     return ( GetDInfo(spec->vRefNum, spec->parID, (StringPtr)spec->name, fndrInfo) );
  758. }
  759.  
  760. /*****************************************************************************/
  761.  
  762. pascal    OSErr SetDInfo(short vRefNum,
  763.                        long dirID,
  764.                        StringPtr name,
  765.                        const DInfo *fndrInfo)
  766. {
  767.     CInfoPBRec pb;
  768.     Str31 tempName;
  769.     OSErr error;
  770.  
  771.     /* Protection against File Sharing problem */
  772.     if ( (name == NULL) || (name[0] == 0) )
  773.     {
  774.         tempName[0] = 0;
  775.         pb.dirInfo.ioNamePtr = tempName;
  776.         pb.dirInfo.ioFDirIndex = -1;    /* use ioDirID */
  777.     }
  778.     else
  779.     {
  780.         pb.dirInfo.ioNamePtr = name;
  781.         pb.dirInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  782.     }
  783.     pb.dirInfo.ioVRefNum = vRefNum;
  784.     pb.dirInfo.ioDrDirID = dirID;
  785.     error = PBGetCatInfoSync(&pb);
  786.     if ( error == noErr )
  787.     {
  788.         if ( (pb.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  789.         {
  790.             /* it's a directory, set the DInfo */
  791.             if ( pb.dirInfo.ioNamePtr == tempName )
  792.                 pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
  793.             else
  794.                 pb.dirInfo.ioDrDirID = dirID;
  795.             pb.dirInfo.ioDrUsrWds = *fndrInfo;
  796.             error = PBSetCatInfoSync(&pb);
  797.         }
  798.         else
  799.             /* oops, a file was passed */
  800.             error = dirNFErr;
  801.     }
  802.     return ( error );
  803. }
  804.  
  805. /*****************************************************************************/
  806.  
  807. pascal    OSErr FSpSetDInfo(const FSSpec *spec,
  808.                           const DInfo *fndrInfo)
  809. {
  810.     return ( SetDInfo(spec->vRefNum, spec->parID, (StringPtr)spec->name, fndrInfo) );
  811. }
  812.  
  813. /*****************************************************************************/
  814.  
  815. pascal    OSErr    GetDirectoryID(short vRefNum,
  816.                                long dirID,
  817.                                StringPtr name,
  818.                                long *theDirID,
  819.                                Boolean *isDirectory)
  820. {
  821.     CInfoPBRec pb;
  822.     Str31 tempName;
  823.     OSErr error;
  824.  
  825.     /* Protection against File Sharing problem */
  826.     if ( (name == NULL) || (name[0] == 0) )
  827.     {
  828.         tempName[0] = 0;
  829.         pb.hFileInfo.ioNamePtr = tempName;
  830.         pb.hFileInfo.ioFDirIndex = -1;    /* use ioDirID */
  831.     }
  832.     else
  833.     {
  834.         pb.hFileInfo.ioNamePtr = name;
  835.         pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  836.     }
  837.     pb.hFileInfo.ioVRefNum = vRefNum;
  838.     pb.hFileInfo.ioDirID = dirID;
  839.     error = PBGetCatInfoSync(&pb);
  840.     *isDirectory = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
  841.     *theDirID = (*isDirectory) ? pb.dirInfo.ioDrDirID : pb.hFileInfo.ioFlParID;
  842.     return ( error );
  843. }
  844.  
  845. /*****************************************************************************/
  846.  
  847. pascal    OSErr    FSpGetDirectoryID(const FSSpec *spec,
  848.                                   long *theDirID,
  849.                                   Boolean *isDirectory)
  850. {
  851.     return ( GetDirectoryID(spec->vRefNum, spec->parID, (StringPtr)spec->name,
  852.              theDirID, isDirectory) );
  853. }
  854.  
  855. /*****************************************************************************/
  856.  
  857. pascal    OSErr    GetDirName(short vRefNum,
  858.                            long dirID,
  859.                            StringPtr name)
  860. {
  861.     CInfoPBRec pb;
  862.     Str31 tempName;    
  863.  
  864.     /* Protection against File Sharing problem */
  865.     if ( name == NULL )
  866.     {
  867.         tempName[0] = 0;
  868.         pb.dirInfo.ioNamePtr = tempName;
  869.     }
  870.     else
  871.     {
  872.         pb.dirInfo.ioNamePtr = name;
  873.     }
  874.     pb.dirInfo.ioNamePtr = name;
  875.     pb.dirInfo.ioVRefNum = vRefNum;
  876.     pb.dirInfo.ioDrDirID = dirID;
  877.     pb.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  878.     return ( PBGetCatInfoSync(&pb) );
  879. }
  880.  
  881. /*****************************************************************************/
  882.  
  883. pascal    OSErr    GetParentID(short vRefNum,
  884.                             long dirID,
  885.                             StringPtr name,
  886.                             long *parID)
  887. {
  888.     CInfoPBRec pb;
  889.     Str31 tempName;
  890.     OSErr error;
  891.     short realVRefNum;
  892.     
  893.     /* Protection against File Sharing problem */
  894.     if ( (name == NULL) || (name[0] == 0) )
  895.     {
  896.         tempName[0] = 0;
  897.         pb.hFileInfo.ioNamePtr = tempName;
  898.         pb.hFileInfo.ioFDirIndex = -1;    /* use ioDirID */
  899.     }
  900.     else
  901.     {
  902.         pb.hFileInfo.ioNamePtr = name;
  903.         pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  904.     }
  905.     pb.hFileInfo.ioVRefNum = vRefNum;
  906.     pb.hFileInfo.ioDirID = dirID;
  907.     error = PBGetCatInfoSync(&pb);
  908.     if ( error == noErr )
  909.     {
  910.         /*
  911.         **    There's a bug in HFS where the wrong parent dir ID can be
  912.         **    returned if multiple separators are used at the end of a
  913.         **    pathname. For example, if the pathname:
  914.         **        'volumeName:System Folder:Extensions::'
  915.         **    is passed, the directory ID of the Extensions folder is
  916.         **    returned in the ioFlParID field instead of fsRtDirID. Since
  917.         **    multiple separators at the end of a pathname always specifies
  918.         **    a directory, we only need to work-around cases where the
  919.         **    object is a directory and there are multiple separators at
  920.         **    the end of the name parameter.
  921.         */
  922.         if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  923.         {
  924.             /* Its a directory */
  925.             
  926.             /* is there a pathname? */
  927.             if ( pb.hFileInfo.ioNamePtr == name )    
  928.             {
  929.                 /* could it contain multiple separators? */
  930.                 if ( name[0] >= 2 )
  931.                 {
  932.                     /* does it contain multiple separators at the end? */
  933.                     if ( (name[name[0]] == ':') && (name[name[0] - 1] == ':') )
  934.                     {
  935.                         /* OK, then do the extra stuff to get the correct parID */
  936.                         
  937.                         /* Get the real vRefNum (this should not fail) */
  938.                         error = DetermineVRefNum(name, vRefNum, &realVRefNum);
  939.                         if ( error == noErr )
  940.                         {
  941.                             /* we don't need the parent's name, but add protect against File Sharing problem */
  942.                             tempName[0] = 0;
  943.                             pb.dirInfo.ioNamePtr = tempName;
  944.                             pb.dirInfo.ioVRefNum = realVRefNum;
  945.                             /* pb.dirInfo.ioDrDirID already contains the */
  946.                             /* dirID of the directory object */
  947.                             pb.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  948.                             error = PBGetCatInfoSync(&pb);
  949.                             /* now, pb.dirInfo.ioDrParID contains the correct parID */
  950.                         }
  951.                     }
  952.                 }
  953.             }
  954.         }
  955.         
  956.         /* if no errors, then pb.hFileInfo.ioFlParID (pb.dirInfo.ioDrParID) */
  957.         /* contains the parent ID */
  958.         *parID = pb.hFileInfo.ioFlParID;
  959.     }
  960.     return ( error );
  961. }
  962.  
  963. /*****************************************************************************/
  964.  
  965. pascal    OSErr    GetFilenameFromPathname(ConstStr255Param pathname,
  966.                                         Str255 filename)
  967. {
  968.     short    index;
  969.     short    nameEnd;
  970.  
  971.     /* default to no filename */
  972.     filename[0] = 0;
  973.  
  974.     /* check for no pathname */
  975.     if ( pathname == NULL )
  976.         return ( notAFileErr );
  977.     
  978.     /* get string length */
  979.     index = pathname[0];
  980.     
  981.     /* check for empty string */
  982.     if ( index == 0 )
  983.         return ( notAFileErr );
  984.     
  985.     /* skip over last trailing colon (if any) */
  986.     if ( pathname[index] == ':' )
  987.         --index;
  988.  
  989.     /* save the end of the string */
  990.     nameEnd = index;
  991.  
  992.     /* if pathname ends with multiple colons, then this pathname refers */
  993.     /* to a directory, not a file */
  994.     if ( pathname[index] == ':' )
  995.         return ( notAFileErr );
  996.         
  997.     
  998.     /* parse backwards until we find a colon or hit the beginning of the pathname */
  999.     while ( (index != 0) && (pathname[index] != ':') )
  1000.     {
  1001.         --index;
  1002.     }
  1003.     
  1004.     /* if we parsed to the beginning of the pathname and the pathname ended */
  1005.     /* with a colon, then pathname is a full pathname to a volume, not a file */
  1006.     if ( (index == 0) && (pathname[pathname[0]] == ':') )
  1007.         return ( notAFileErr );
  1008.     
  1009.     /* get the filename and return noErr */
  1010.     filename[0] = (char)(nameEnd - index);
  1011.     BlockMoveData(&pathname[index+1], &filename[1], nameEnd - index);
  1012.     return ( noErr );
  1013. }
  1014.  
  1015. /*****************************************************************************/
  1016.  
  1017. pascal    OSErr    GetObjectLocation(short vRefNum,
  1018.                                   long dirID,
  1019.                                   StringPtr pathname,
  1020.                                   short *realVRefNum,
  1021.                                   long *realParID,
  1022.                                   Str255 realName,
  1023.                                   Boolean *isDirectory)
  1024. {
  1025.     OSErr error;
  1026.     UniversalFMPB pb;
  1027.     Str255 tempPathname;
  1028.     
  1029.     /* clear results */
  1030.     *realVRefNum = 0;
  1031.     *realParID = 0;
  1032.     realName[0] = 0;
  1033.     
  1034.     /*
  1035.     **    Get the real vRefNum
  1036.     */
  1037.     pb.hPB.volumeParam.ioVRefNum = vRefNum;
  1038.     if ( pathname == NULL )
  1039.     {
  1040.         pb.hPB.volumeParam.ioNamePtr = NULL;
  1041.         pb.hPB.volumeParam.ioVolIndex = 0;    /* use ioVRefNum only */
  1042.     }
  1043.     else
  1044.     {
  1045.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  1046.         pb.hPB.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  1047.         pb.hPB.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  1048.     }
  1049.     error = PBHGetVInfoSync(&pb.hPB);
  1050.  
  1051.     if ( error == noErr )
  1052.     {
  1053.         *realVRefNum = pb.hPB.volumeParam.ioVRefNum;    /* we have the real vRefNum */
  1054.         
  1055.         /*
  1056.         **    Determine if the object already exists and if so,
  1057.         **    get the real parent directory ID if it's a file
  1058.         */
  1059.         
  1060.         /* Protection against File Sharing problem */
  1061.         if ( (pathname == NULL) || (pathname[0] == 0) )
  1062.         {
  1063.             tempPathname[0] = 0;
  1064.             pb.ciPB.hFileInfo.ioNamePtr = tempPathname;
  1065.             pb.ciPB.hFileInfo.ioFDirIndex = -1;    /* use ioDirID */
  1066.         }
  1067.         else
  1068.         {
  1069.             pb.ciPB.hFileInfo.ioNamePtr = pathname;
  1070.             pb.ciPB.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1071.         }
  1072.         pb.ciPB.hFileInfo.ioVRefNum = vRefNum;
  1073.         pb.ciPB.hFileInfo.ioDirID = dirID;
  1074.         error = PBGetCatInfoSync(&pb.ciPB);
  1075.         
  1076.         if ( error == noErr )
  1077.         {
  1078.             /*
  1079.             **    The file system object is present and we have the file's real parID
  1080.             */
  1081.             
  1082.             /*    Is it a directory or a file? */
  1083.             *isDirectory = (pb.ciPB.hFileInfo.ioFlAttrib & ioDirMask) != 0;
  1084.             if ( *isDirectory )
  1085.             {
  1086.                 /*
  1087.                 **    It's a directory, get its name and parent dirID, and then we're done
  1088.                 */
  1089.                 
  1090.                 pb.ciPB.dirInfo.ioNamePtr = realName;
  1091.                 pb.ciPB.dirInfo.ioVRefNum = *realVRefNum;
  1092.                 /* pb.ciPB.dirInfo.ioDrDirID already contains the dirID of the directory object */
  1093.                 pb.ciPB.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  1094.                 error = PBGetCatInfoSync(&pb.ciPB);
  1095.                 
  1096.                 /* get the parent ID here, because the file system can return the */
  1097.                 /* wrong parent ID from the last call. */
  1098.                 *realParID = pb.ciPB.dirInfo.ioDrParID;
  1099.             }
  1100.             else
  1101.             {
  1102.                 /*
  1103.                 **    It's a file - use the parent directory ID from the last call
  1104.                 **    to GetCatInfoparse, get the file name, and then we're done
  1105.                 */
  1106.                 *realParID = pb.ciPB.hFileInfo.ioFlParID;    
  1107.                 error = GetFilenameFromPathname(pathname, realName);
  1108.             }
  1109.         }
  1110.         else if ( error == fnfErr )
  1111.         {
  1112.             /*
  1113.             **    The file system object is not present - see if its parent is present
  1114.             */
  1115.             
  1116.             /*
  1117.             **    Parse to get the object name from end of pathname
  1118.             */
  1119.             error = GetFilenameFromPathname(pathname, realName);
  1120.             
  1121.             /* if we can't get the object name from the end, we can't continue */
  1122.             if ( error == noErr )
  1123.             {
  1124.                 /*
  1125.                 **    What we want now is the pathname minus the object name
  1126.                 **    for example:
  1127.                 **    if pathname is 'vol:dir:file' tempPathname becomes 'vol:dir:'
  1128.                 **    if pathname is 'vol:dir:file:' tempPathname becomes 'vol:dir:'
  1129.                 **    if pathname is ':dir:file' tempPathname becomes ':dir:'
  1130.                 **    if pathname is ':dir:file:' tempPathname becomes ':dir:'
  1131.                 **    if pathname is ':file' tempPathname becomes ':'
  1132.                 **    if pathname is 'file or file:' tempPathname becomes ''
  1133.                 */
  1134.                 
  1135.                 /* get a copy of the pathname */
  1136.                 BlockMoveData(pathname, tempPathname, pathname[0] + 1);
  1137.                 
  1138.                 /* remove the object name */
  1139.                 tempPathname[0] -= realName[0];
  1140.                 /* and the trailing colon (if any) */
  1141.                 if ( pathname[pathname[0]] == ':' )
  1142.                     --tempPathname[0];
  1143.                 
  1144.                 /* OK, now get the parent's directory ID */
  1145.                 
  1146.                 /* Protection against File Sharing problem */
  1147.                 pb.ciPB.hFileInfo.ioNamePtr = (StringPtr)tempPathname;
  1148.                 if ( tempPathname[0] != 0 )
  1149.                 {
  1150.                     pb.ciPB.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1151.                 }
  1152.                 else
  1153.                 {
  1154.                     pb.ciPB.hFileInfo.ioFDirIndex = -1;    /* use ioDirID */
  1155.                 }
  1156.                 pb.ciPB.hFileInfo.ioVRefNum = vRefNum;
  1157.                 pb.ciPB.hFileInfo.ioDirID = dirID;
  1158.                 error = PBGetCatInfoSync(&pb.ciPB);
  1159.                 *realParID = pb.ciPB.dirInfo.ioDrDirID;
  1160.  
  1161.                 *isDirectory = false;    /* we don't know what the object is really going to be */
  1162.             }
  1163.             
  1164.             if ( error != noErr )
  1165.                 error = dirNFErr;    /* couldn't find parent directory */
  1166.             else
  1167.                 error = fnfErr;    /* we found the parent, but not the file */
  1168.         }
  1169.     }
  1170.     return ( error );
  1171. }
  1172.  
  1173. /*****************************************************************************/
  1174.  
  1175. pascal    OSErr    GetDirItems(short vRefNum,
  1176.                             long dirID,
  1177.                             StringPtr name,
  1178.                             Boolean getFiles,
  1179.                             Boolean getDirectories,
  1180.                             FSSpecPtr items,
  1181.                             short reqItemCount,
  1182.                             short *actItemCount,
  1183.                             short *itemIndex) /* start with 1, then use what's returned */
  1184. {
  1185.     CInfoPBRec pb;
  1186.     OSErr error = noErr;
  1187.     long theDirID;
  1188.     Boolean isDirectory;
  1189.     FSSpec *endItemsArray = items + reqItemCount;
  1190.  
  1191.     /* NOTE: If I could be sure that the caller passed a real vRefNum and real directory */
  1192.     /* to this routine, I could rip out calls to DetermineVRefNum and GetDirectoryID and this */
  1193.     /* routine would be much faster because of the overhead of DetermineVRefNum and */
  1194.     /* GetDirectoryID and because GetDirectoryID blows away the directory index hint the Macintosh */
  1195.     /* file system keeps for indexed calls. I can't be sure, so for maximum throughput, */
  1196.     /* pass a big array of FSSpecs so you can get the directory's contents with few calls */
  1197.     /* to this routine. */
  1198.     
  1199.     /* get the real volume reference number */
  1200.     error = DetermineVRefNum(name, vRefNum, &pb.hFileInfo.ioVRefNum);
  1201.     if ( error != noErr )
  1202.         return ( error );
  1203.     
  1204.     /* and the real directory ID of this directory (and make sure it IS a directory) */
  1205.     error = GetDirectoryID(vRefNum, dirID, name, &theDirID, &isDirectory);
  1206.     if ( error != noErr )
  1207.         return ( error );
  1208.     else if ( !isDirectory )
  1209.         return ( dirNFErr );
  1210.  
  1211.  
  1212.     *actItemCount = 0;
  1213.     for ( ; (items < endItemsArray) && (error == noErr); )
  1214.     {
  1215.         pb.hFileInfo.ioNamePtr = (StringPtr) &items->name;
  1216.         pb.hFileInfo.ioDirID = theDirID;
  1217.         pb.hFileInfo.ioFDirIndex = *itemIndex;
  1218.         error = PBGetCatInfoSync(&pb);
  1219.         if ( error == noErr )
  1220.         {
  1221.             items->parID = pb.hFileInfo.ioFlParID;    /* return item's parID */
  1222.             items->vRefNum = pb.hFileInfo.ioVRefNum;    /* return item's vRefNum */
  1223.             ++*itemIndex;    /* prepare to get next item in directory */
  1224.             
  1225.             if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  1226.             {
  1227.                 if ( getDirectories )
  1228.                 {
  1229.                     ++*actItemCount; /* keep this item */
  1230.                     ++items; /* point to next item */
  1231.                 }
  1232.             }
  1233.             else
  1234.             {
  1235.                 if ( getFiles )
  1236.                 {
  1237.                     ++*actItemCount; /* keep this item */
  1238.                     ++items; /* point to next item */
  1239.                 }
  1240.             }
  1241.         }
  1242.     }
  1243.     return ( error );
  1244. }
  1245.  
  1246. /*****************************************************************************/
  1247.  
  1248. static    void    DeleteLevel(long dirToDelete,
  1249.                             DeleteEnumGlobalsPtr theGlobals)
  1250. {
  1251.     long savedDir;
  1252.     
  1253.     do
  1254.     {
  1255.         /* prepare to delete directory */
  1256.         theGlobals->myPB.ciPB.dirInfo.ioNamePtr = (StringPtr)&theGlobals->itemName;
  1257.         theGlobals->myPB.ciPB.dirInfo.ioFDirIndex = 1;    /* get first item */
  1258.         theGlobals->myPB.ciPB.dirInfo.ioDrDirID = dirToDelete;    /* in this directory */
  1259.         theGlobals->error = PBGetCatInfoSync(&(theGlobals->myPB.ciPB));
  1260.         if ( theGlobals->error == noErr )
  1261.         {
  1262.             savedDir = dirToDelete;
  1263.             /* We have an item.  Is it a file or directory? */
  1264.             if ( (theGlobals->myPB.ciPB.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  1265.             {
  1266.                 /* it's a directory */
  1267.                 savedDir = theGlobals->myPB.ciPB.dirInfo.ioDrDirID;    /* save dirID of directory instead */
  1268.                 DeleteLevel(theGlobals->myPB.ciPB.dirInfo.ioDrDirID, theGlobals);    /* Delete its contents */
  1269.                 theGlobals->myPB.ciPB.dirInfo.ioNamePtr = NULL;    /* prepare to delete directory */
  1270.             }
  1271.             if ( theGlobals->error == noErr )
  1272.             {
  1273.                 theGlobals->myPB.ciPB.dirInfo.ioDrDirID = savedDir;    /* restore dirID */
  1274.                 theGlobals->myPB.hPB.fileParam.ioFVersNum = 0;    /* just in case it's used on an MFS volume... */
  1275.                 theGlobals->error = PBHDeleteSync(&(theGlobals->myPB.hPB));    /* delete this item */
  1276.                 if ( theGlobals->error == fLckdErr )
  1277.                 {
  1278.                     (void) PBHRstFLockSync(&(theGlobals->myPB.hPB));    /* unlock it */
  1279.                     theGlobals->error = PBHDeleteSync(&(theGlobals->myPB.hPB));    /* and try again */
  1280.                 }
  1281.             }
  1282.         }
  1283.     } while ( theGlobals->error == noErr );
  1284.     
  1285.     if ( theGlobals->error == fnfErr )
  1286.         theGlobals->error = noErr;
  1287. }
  1288.  
  1289. /*****************************************************************************/
  1290.  
  1291. pascal    OSErr    DeleteDirectoryContents(short vRefNum,
  1292.                                          long dirID,
  1293.                                         StringPtr name)
  1294. {
  1295.     DeleteEnumGlobals theGlobals;
  1296.     Boolean    isDirectory;
  1297.     OSErr error;
  1298.  
  1299.     /*  Get the real dirID and make sure it is a directory. */
  1300.     error = GetDirectoryID(vRefNum, dirID, name, &dirID, &isDirectory);
  1301.     if ( error != noErr )
  1302.         return ( error );
  1303.     if ( !isDirectory )
  1304.         return ( dirNFErr );
  1305.     
  1306.     /* Get the real vRefNum */
  1307.     error = DetermineVRefNum(name, vRefNum, &vRefNum);
  1308.     if ( error != noErr )
  1309.         return ( error );
  1310.     
  1311.     /* Set up the globals we need to access from the recursive routine. */
  1312.     theGlobals.myPB.ciPB.dirInfo.ioVRefNum = vRefNum;
  1313.         
  1314.     /* Here we go into recursion land... */
  1315.     DeleteLevel(dirID, &theGlobals);
  1316.     return ( theGlobals.error );
  1317. }
  1318.  
  1319. /*****************************************************************************/
  1320.  
  1321. pascal    OSErr    DeleteDirectory(short vRefNum,
  1322.                                 long dirID,
  1323.                                 StringPtr name)
  1324. {
  1325.     OSErr error;
  1326.     
  1327.     /* Make sure a directory was specified and then delete its contents */
  1328.     error = DeleteDirectoryContents(vRefNum, dirID, name);
  1329.     if ( error == noErr )
  1330.     {
  1331.         error = HDelete(vRefNum, dirID, name);
  1332.         if ( error == fLckdErr )
  1333.         {
  1334.             (void) HRstFLock(vRefNum, dirID, name);    /* unlock the directory locked by AppleShare */
  1335.             error = HDelete(vRefNum, dirID, name);    /* and try again */
  1336.         }
  1337.     }
  1338.     return ( error );
  1339. }
  1340.  
  1341. /*****************************************************************************/
  1342.  
  1343. pascal    OSErr    CheckObjectLock(short vRefNum,
  1344.                                 long dirID,
  1345.                                 StringPtr name)
  1346. {
  1347.     CInfoPBRec pb;
  1348.     Str31 tempName;
  1349.     OSErr error;
  1350.     
  1351.     /* Protection against File Sharing problem */
  1352.     if ( (name == NULL) || (name[0] == 0) )
  1353.     {
  1354.         tempName[0] = 0;
  1355.         pb.hFileInfo.ioNamePtr = tempName;
  1356.         pb.hFileInfo.ioFDirIndex = -1;    /* use ioDirID */
  1357.     }
  1358.     else
  1359.     {
  1360.         pb.hFileInfo.ioNamePtr = name;
  1361.         pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1362.     }
  1363.     pb.hFileInfo.ioVRefNum = vRefNum;
  1364.     pb.hFileInfo.ioDirID = dirID;
  1365.     error = PBGetCatInfoSync(&pb);
  1366.     
  1367.     if ( error == noErr )
  1368.     {
  1369.         /* check locked bit */
  1370.         if ( (pb.hFileInfo.ioFlAttrib & 0x01) != 0 )
  1371.             error = fLckdErr;
  1372.     }
  1373.     return ( error );
  1374. }
  1375.  
  1376. /*****************************************************************************/
  1377.  
  1378. pascal    OSErr    FSpCheckObjectLock(const FSSpec *spec)
  1379. {
  1380.     return ( CheckObjectLock(spec->vRefNum, spec->parID, (StringPtr)spec->name) );
  1381. }
  1382.  
  1383. /*****************************************************************************/
  1384.  
  1385. pascal    OSErr    GetFileSize(short vRefNum,
  1386.                             long dirID,
  1387.                             ConstStr255Param fileName,
  1388.                             long *dataSize,
  1389.                             long *rsrcSize)
  1390. {
  1391.     HParamBlockRec pb;
  1392.     OSErr error;
  1393.     
  1394.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  1395.     pb.fileParam.ioVRefNum = vRefNum;
  1396.     pb.fileParam.ioFVersNum = 0;
  1397.     pb.fileParam.ioDirID = dirID;
  1398.     pb.fileParam.ioFDirIndex = 0;
  1399.     error = PBHGetFInfoSync(&pb);
  1400.     if ( error == noErr )
  1401.     {
  1402.         *dataSize = pb.fileParam.ioFlLgLen;
  1403.         *rsrcSize = pb.fileParam.ioFlRLgLen;
  1404.     }
  1405.     return ( error );
  1406. }
  1407.  
  1408. /*****************************************************************************/
  1409.  
  1410. pascal    OSErr    FSpGetFileSize(const FSSpec *spec,
  1411.                                long *dataSize,
  1412.                                long *rsrcSize)
  1413. {
  1414.     return ( GetFileSize(spec->vRefNum, spec->parID, spec->name, dataSize, rsrcSize) );
  1415. }
  1416.  
  1417. /*****************************************************************************/
  1418.  
  1419. pascal    OSErr    BumpDate(short vRefNum,
  1420.                          long dirID,
  1421.                          StringPtr name)
  1422. /* Given a file or directory, change its modification date to the current date/time. */
  1423. {
  1424.     CInfoPBRec pb;
  1425.     Str31 tempName;
  1426.     OSErr error;
  1427.     unsigned long secs;
  1428.  
  1429.     /* Protection against File Sharing problem */
  1430.     if ( (name == NULL) || (name[0] == 0) )
  1431.     {
  1432.         tempName[0] = 0;
  1433.         pb.hFileInfo.ioNamePtr = tempName;
  1434.         pb.hFileInfo.ioFDirIndex = -1;    /* use ioDirID */
  1435.     }
  1436.     else
  1437.     {
  1438.         pb.hFileInfo.ioNamePtr = name;
  1439.         pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1440.     }
  1441.     pb.hFileInfo.ioVRefNum = vRefNum;
  1442.     pb.hFileInfo.ioDirID = dirID;
  1443.     error = PBGetCatInfoSync(&pb);
  1444.     if ( error == noErr )
  1445.     {
  1446.         GetDateTime(&secs);
  1447.         /* set mod date to current date, or one second into the future
  1448.             if mod date = current date */
  1449.         pb.hFileInfo.ioFlMdDat = (secs == pb.hFileInfo.ioFlMdDat) ? (++secs) : (secs);
  1450.         if ( pb.dirInfo.ioNamePtr == tempName )
  1451.             pb.hFileInfo.ioDirID = pb.hFileInfo.ioFlParID;
  1452.         else
  1453.             pb.hFileInfo.ioDirID = dirID;
  1454.         error = PBSetCatInfoSync(&pb);
  1455.     }
  1456.     return ( error );
  1457. }
  1458.  
  1459. /*****************************************************************************/
  1460.  
  1461. pascal    OSErr    FSpBumpDate(const FSSpec *spec)
  1462. {
  1463.     return ( BumpDate(spec->vRefNum, spec->parID, (StringPtr)spec->name) );
  1464. }
  1465.  
  1466. /*****************************************************************************/
  1467.  
  1468. pascal    OSErr    ChangeCreatorType(short vRefNum,
  1469.                                   long dirID,
  1470.                                   ConstStr255Param name,
  1471.                                   OSType creator,
  1472.                                   OSType fileType)
  1473. {
  1474.     CInfoPBRec pb;
  1475.     OSErr error;
  1476.     short realVRefNum;
  1477.     long parID;
  1478.  
  1479.     pb.hFileInfo.ioNamePtr = (StringPtr)name;
  1480.     pb.hFileInfo.ioVRefNum = vRefNum;
  1481.     pb.hFileInfo.ioDirID = dirID;
  1482.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1483.     error = PBGetCatInfoSync(&pb);
  1484.     if ( error == noErr )
  1485.     {
  1486.         if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )    /* if directory */
  1487.             return ( notAFileErr );            /* do nothing and return error */
  1488.             
  1489.         parID = pb.hFileInfo.ioFlParID;    /* save parent dirID for BumpDate call */
  1490.  
  1491.         /* If creator not 0x00000000, change creator */
  1492.         if ( creator != (OSType)0x00000000 )
  1493.             pb.hFileInfo.ioFlFndrInfo.fdCreator = creator;
  1494.         
  1495.         /* If fileType not 0x00000000, change fileType */
  1496.         if ( fileType != (OSType)0x00000000 )
  1497.             pb.hFileInfo.ioFlFndrInfo.fdType = fileType;
  1498.             
  1499.         pb.hFileInfo.ioDirID = dirID;
  1500.         error = PBSetCatInfoSync(&pb);    /* now, save the new information back to disk */
  1501.  
  1502.         if ( (error == noErr) && (parID != fsRtParID) ) /* can't bump fsRtParID */
  1503.         {
  1504.             /* get the real vRefNum in case a full pathname was passed */
  1505.             error = DetermineVRefNum((StringPtr)name, vRefNum, &realVRefNum);
  1506.             if ( error == noErr )
  1507.             {
  1508.                 error = BumpDate(realVRefNum, parID, NULL);
  1509.                     /* and bump the parent directory's mod date to wake up the Finder */
  1510.                     /* to the change we just made */
  1511.             }
  1512.         }
  1513.     }
  1514.     return ( error );
  1515. }
  1516.  
  1517. /*****************************************************************************/
  1518.  
  1519. pascal    OSErr    FSpChangeCreatorType(const FSSpec *spec,
  1520.                                      OSType creator,
  1521.                                      OSType fileType)
  1522. {
  1523.     return ( ChangeCreatorType(spec->vRefNum, spec->parID, spec->name, creator, fileType) );
  1524. }
  1525.  
  1526. /*****************************************************************************/
  1527.  
  1528. pascal    OSErr    ChangeFDFlags(short vRefNum,
  1529.                               long dirID,
  1530.                               StringPtr name,
  1531.                               Boolean    setBits,
  1532.                               unsigned short flagBits)
  1533. {
  1534.     CInfoPBRec pb;
  1535.     Str31 tempName;
  1536.     OSErr error;
  1537.     short realVRefNum;
  1538.     long parID;
  1539.  
  1540.     /* Protection against File Sharing problem */
  1541.     if ( (name == NULL) || (name[0] == 0) )
  1542.     {
  1543.         tempName[0] = 0;
  1544.         pb.hFileInfo.ioNamePtr = tempName;
  1545.         pb.hFileInfo.ioFDirIndex = -1;    /* use ioDirID */
  1546.     }
  1547.     else
  1548.     {
  1549.         pb.hFileInfo.ioNamePtr = name;
  1550.         pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1551.     }
  1552.     pb.hFileInfo.ioVRefNum = vRefNum;
  1553.     pb.hFileInfo.ioDirID = dirID;
  1554.     error = PBGetCatInfoSync(&pb);
  1555.     if ( error == noErr )
  1556.     {
  1557.         parID = pb.hFileInfo.ioFlParID;    /* save parent dirID for BumpDate call */
  1558.  
  1559.         pb.hFileInfo.ioFlFndrInfo.fdFlags = ( setBits ) ? 
  1560.             ( pb.hFileInfo.ioFlFndrInfo.fdFlags | flagBits ) : /* OR in the bits */
  1561.             ( pb.hFileInfo.ioFlFndrInfo.fdFlags & (0xffff ^ flagBits) ); /* AND out the bits */
  1562.                 /* set or clear the appropriate bits in the Finder flags */
  1563.             
  1564.         if ( pb.dirInfo.ioNamePtr == tempName )
  1565.             pb.hFileInfo.ioDirID = pb.hFileInfo.ioFlParID;
  1566.         else
  1567.             pb.hFileInfo.ioDirID = dirID;
  1568.         error = PBSetCatInfoSync(&pb);    /* now, save the new information back to disk */
  1569.  
  1570.         if ( (error == noErr) && (parID != fsRtParID) ) /* can't bump fsRtParID */
  1571.         {
  1572.             /* get the real vRefNum in case a full pathname was passed */
  1573.             error = DetermineVRefNum((StringPtr)name, vRefNum, &realVRefNum);
  1574.             if ( error == noErr )
  1575.             {
  1576.                 error = BumpDate(realVRefNum, parID, NULL);
  1577.                     /* and bump the parent directory's mod date to wake up the Finder */
  1578.                     /* to the change we just made */
  1579.             }
  1580.         }
  1581.     }
  1582.     return ( error );
  1583. }
  1584.  
  1585. /*****************************************************************************/
  1586.  
  1587. pascal    OSErr    FSpChangeFDFlags(const FSSpec *spec,
  1588.                                  Boolean setBits,
  1589.                                  unsigned short flagBits)
  1590. {
  1591.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, setBits, flagBits) );
  1592. }
  1593.  
  1594. /*****************************************************************************/
  1595.  
  1596. pascal    OSErr    SetIsInvisible(short vRefNum,
  1597.                                long dirID,
  1598.                                StringPtr name)
  1599.     /* Given a file or directory, make it invisible. */
  1600. {
  1601.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x4000) );
  1602. }
  1603.  
  1604. /*****************************************************************************/
  1605.  
  1606. pascal    OSErr    FSpSetIsInvisible(const FSSpec *spec)
  1607.     /* Given a file or directory, make it invisible. */
  1608. {
  1609.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x4000) );
  1610. }
  1611.  
  1612. /*****************************************************************************/
  1613.  
  1614. pascal    OSErr    ClearIsInvisible(short vRefNum,
  1615.                                  long dirID,
  1616.                                  StringPtr name)
  1617.     /* Given a file or directory, make it visible. */
  1618. {
  1619.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x4000) );
  1620. }
  1621.  
  1622. /*****************************************************************************/
  1623.  
  1624. pascal    OSErr    FSpClearIsInvisible(const FSSpec *spec)
  1625.     /* Given a file or directory, make it visible. */
  1626. {
  1627.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x4000) );
  1628. }
  1629.  
  1630. /*****************************************************************************/
  1631.  
  1632. pascal    OSErr    SetNameLocked(short vRefNum,
  1633.                               long dirID,
  1634.                               StringPtr name)
  1635.     /* Given a file or directory, lock its name. */
  1636. {
  1637.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x1000) );
  1638. }
  1639.  
  1640. /*****************************************************************************/
  1641.  
  1642. pascal    OSErr    FSpSetNameLocked(const FSSpec *spec)
  1643.     /* Given a file or directory, lock its name. */
  1644. {
  1645.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x1000) );
  1646. }
  1647.  
  1648. /*****************************************************************************/
  1649.  
  1650. pascal    OSErr    ClearNameLocked(short vRefNum,
  1651.                                 long dirID,
  1652.                                 StringPtr name)
  1653.     /* Given a file or directory, unlock its name. */
  1654. {
  1655.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x1000) );
  1656. }
  1657.  
  1658. /*****************************************************************************/
  1659.  
  1660. pascal    OSErr    FSpClearNameLocked(const FSSpec *spec)
  1661.     /* Given a file or directory, unlock its name. */
  1662. {
  1663.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x1000) );
  1664. }
  1665.  
  1666. /*****************************************************************************/
  1667.  
  1668. pascal    OSErr    SetIsStationery(short vRefNum,
  1669.                                 long dirID,
  1670.                                 ConstStr255Param name)
  1671.     /* Given a file, make it a stationery pad. */
  1672. {
  1673.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, true, 0x0800) );
  1674. }
  1675.  
  1676. /*****************************************************************************/
  1677.  
  1678. pascal    OSErr    FSpSetIsStationery(const FSSpec *spec)
  1679.     /* Given a file, make it a stationery pad. */
  1680. {
  1681.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x0800) );
  1682. }
  1683.  
  1684. /*****************************************************************************/
  1685.  
  1686. pascal    OSErr    ClearIsStationery(short vRefNum,
  1687.                                   long dirID,
  1688.                                   ConstStr255Param name)
  1689.     /* Given a file, clear the stationery bit. */
  1690. {
  1691.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, false, 0x0800) );
  1692. }
  1693.  
  1694. /*****************************************************************************/
  1695.  
  1696. pascal    OSErr    FSpClearIsStationery(const FSSpec *spec)
  1697.     /* Given a file, clear the stationery bit. */
  1698. {
  1699.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0800) );
  1700. }
  1701.  
  1702. /*****************************************************************************/
  1703.  
  1704. pascal    OSErr    SetHasCustomIcon(short vRefNum,
  1705.                                  long dirID,
  1706.                                  StringPtr name)
  1707.     /* Given a file or directory, indicate that it has a custom icon. */
  1708. {
  1709.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x0400) );
  1710. }
  1711.  
  1712. /*****************************************************************************/
  1713.  
  1714. pascal    OSErr    FSpSetHasCustomIcon(const FSSpec *spec)
  1715.     /* Given a file or directory, indicate that it has a custom icon. */
  1716. {
  1717.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x0400) );
  1718. }
  1719.  
  1720. /*****************************************************************************/
  1721.  
  1722. pascal    OSErr    ClearHasCustomIcon(short vRefNum,
  1723.                                    long dirID,
  1724.                                    StringPtr name)
  1725.     /* Given a file or directory, indicate that it does not have a custom icon. */
  1726. {
  1727.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x0400) );
  1728. }
  1729.  
  1730. /*****************************************************************************/
  1731.  
  1732. pascal    OSErr    FSpClearHasCustomIcon(const FSSpec *spec)
  1733.     /* Given a file or directory, indicate that it does not have a custom icon. */
  1734. {
  1735.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0400) );
  1736. }
  1737.  
  1738. /*****************************************************************************/
  1739.  
  1740. pascal    OSErr    ClearHasBeenInited(short vRefNum,
  1741.                                    long dirID,
  1742.                                    StringPtr name)
  1743.     /* Given a file, clear its "has been inited" bit. */
  1744. {
  1745.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, false, 0x0100) );
  1746. }
  1747.  
  1748. /*****************************************************************************/
  1749.  
  1750. pascal    OSErr    FSpClearHasBeenInited(const FSSpec *spec)
  1751.     /* Given a file, clear its "has been inited" bit. */
  1752. {
  1753.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0100) );
  1754. }
  1755.  
  1756. /*****************************************************************************/
  1757.  
  1758. pascal    OSErr    CopyFileMgrAttributes(short srcVRefNum,
  1759.                                       long srcDirID,
  1760.                                       StringPtr srcName,
  1761.                                       short dstVRefNum,
  1762.                                       long dstDirID,
  1763.                                       StringPtr dstName,
  1764.                                       Boolean copyLockBit)
  1765. {
  1766.     UniversalFMPB pb;
  1767.     Str31 tempName;
  1768.     OSErr error;
  1769.     Boolean objectIsDirectory;
  1770.  
  1771.     pb.ciPB.hFileInfo.ioVRefNum = srcVRefNum;
  1772.     pb.ciPB.hFileInfo.ioDirID = srcDirID;
  1773.  
  1774.     /* Protection against File Sharing problem */
  1775.     if ( (srcName == NULL) || (srcName[0] == 0) )
  1776.     {
  1777.         tempName[0] = 0;
  1778.         pb.ciPB.hFileInfo.ioNamePtr = tempName;
  1779.         pb.ciPB.hFileInfo.ioFDirIndex = -1;    /* use ioDirID */
  1780.     }
  1781.     else
  1782.     {
  1783.         pb.ciPB.hFileInfo.ioNamePtr = srcName;
  1784.         pb.ciPB.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1785.     }
  1786.     error = PBGetCatInfoSync(&pb.ciPB);
  1787.     if ( error == noErr )
  1788.     {
  1789.         objectIsDirectory = ( (pb.ciPB.hFileInfo.ioFlAttrib & ioDirMask) != 0 );
  1790.         pb.ciPB.hFileInfo.ioVRefNum = dstVRefNum;
  1791.         pb.ciPB.hFileInfo.ioDirID = dstDirID;
  1792.         if ( (dstName != NULL) && (dstName[0] == 0) )
  1793.             pb.ciPB.hFileInfo.ioNamePtr = NULL;
  1794.         else
  1795.             pb.ciPB.hFileInfo.ioNamePtr = dstName;
  1796.         /* don't copy the hasBeenInited bit */
  1797.         pb.ciPB.hFileInfo.ioFlFndrInfo.fdFlags = ( pb.ciPB.hFileInfo.ioFlFndrInfo.fdFlags & 0xfeff );
  1798.         error = PBSetCatInfoSync(&pb.ciPB);
  1799.         if ( (error == noErr) && (copyLockBit) && ((pb.ciPB.hFileInfo.ioFlAttrib & 0x01) != 0) )
  1800.         {
  1801.             pb.hPB.fileParam.ioFVersNum = 0;
  1802.             error = PBHSetFLockSync(&pb.hPB);
  1803.             if ( (error != noErr) && (objectIsDirectory) )
  1804.                 error = noErr; /* ignore lock errors if destination is directory */
  1805.         }
  1806.     }
  1807.     return ( error );
  1808. }
  1809.  
  1810. /*****************************************************************************/
  1811.  
  1812. pascal    OSErr    FSpCopyFileMgrAttributes(const FSSpec *srcSpec,
  1813.                                          const FSSpec *dstSpec,
  1814.                                          Boolean copyLockBit)
  1815. {
  1816.     return ( CopyFileMgrAttributes(srcSpec->vRefNum, srcSpec->parID, (StringPtr)srcSpec->name,
  1817.                                    dstSpec->vRefNum, dstSpec->parID, (StringPtr)dstSpec->name,
  1818.                                    copyLockBit) );
  1819. }
  1820.  
  1821. /*****************************************************************************/
  1822.  
  1823. pascal    OSErr    HOpenAware(short vRefNum,
  1824.                            long dirID,
  1825.                            ConstStr255Param fileName,
  1826.                            short denyModes,
  1827.                            short *refNum)
  1828. {
  1829.     HParamBlockRec pb;
  1830.     OSErr error;
  1831.     GetVolParmsInfoBuffer volParmsInfo;
  1832.     long infoSize = sizeof(GetVolParmsInfoBuffer);
  1833.  
  1834.     pb.ioParam.ioMisc = NULL;
  1835.     pb.fileParam.ioFVersNum = 0;
  1836.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  1837.     pb.fileParam.ioVRefNum = vRefNum;
  1838.     pb.fileParam.ioDirID = dirID;
  1839.  
  1840.     /* get volume attributes */
  1841.     /* this preflighting is needed because Foreign File Access based file systems don't */
  1842.     /* return the correct error result to the OpenDeny call */
  1843.     error = HGetVolParms((StringPtr)fileName, vRefNum, &volParmsInfo, &infoSize);
  1844.     if ( error == noErr )
  1845.     {
  1846.         /* if volume supports OpenDeny, use it and return */
  1847.         if ( hasOpenDeny(volParmsInfo) )
  1848.         {
  1849.             pb.accessParam.ioDenyModes = denyModes;
  1850.             error = PBHOpenDenySync(&pb);
  1851.             *refNum = pb.ioParam.ioRefNum;
  1852.             return ( error );
  1853.         }
  1854.     }
  1855.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  1856.         return ( error );
  1857.     
  1858.     /* OpenDeny isn't supported, so try File Manager Open functions */
  1859.     
  1860.     /* If request includes write permission, then see if the volume is */
  1861.     /* locked by hardware or software. The HFS file system doesn't check */
  1862.     /* for this when a file is opened - you only find out later when you */
  1863.     /* try to write and the write fails with a wPrErr or a vLckdErr. */
  1864.     
  1865.     if ( (denyModes & dmWr) != 0 )
  1866.     {
  1867.         error = CheckVolLock((StringPtr)fileName, vRefNum);
  1868.         if ( error != noErr )
  1869.             return ( error );
  1870.     }
  1871.     
  1872.     /* Set File Manager permissions to closest thing possible */
  1873.     pb.ioParam.ioPermssn = ((denyModes == dmWr) || (denyModes == dmRdWr)) ?
  1874.                            (fsRdWrShPerm) :
  1875.                            (denyModes % 4);
  1876.  
  1877.     error = PBHOpenDFSync(&pb);                /* Try OpenDF */
  1878.     if ( error == paramErr )
  1879.         error = PBHOpenSync(&pb);            /* OpenDF not supported, so try Open */
  1880.     *refNum = pb.ioParam.ioRefNum;
  1881.     return ( error );
  1882. }
  1883.  
  1884. /*****************************************************************************/
  1885.  
  1886. pascal    OSErr    FSpOpenAware(const FSSpec *spec,
  1887.                              short denyModes,
  1888.                              short *refNum)
  1889. {
  1890.     return ( HOpenAware(spec->vRefNum, spec->parID, spec->name, denyModes, refNum) );
  1891. }
  1892.  
  1893. /*****************************************************************************/
  1894.  
  1895. pascal    OSErr    HOpenRFAware(short vRefNum,
  1896.                              long dirID,
  1897.                              ConstStr255Param fileName,
  1898.                              short denyModes,
  1899.                              short *refNum)
  1900. {
  1901.     HParamBlockRec pb;
  1902.     OSErr error;
  1903.     GetVolParmsInfoBuffer volParmsInfo;
  1904.     long infoSize = sizeof(GetVolParmsInfoBuffer);
  1905.  
  1906.     pb.ioParam.ioMisc = NULL;
  1907.     pb.fileParam.ioFVersNum = 0;
  1908.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  1909.     pb.fileParam.ioVRefNum = vRefNum;
  1910.     pb.fileParam.ioDirID = dirID;
  1911.  
  1912.     /* get volume attributes */
  1913.     /* this preflighting is needed because Foreign File Access based file systems don't */
  1914.     /* return the correct error result to the OpenRFDeny call */
  1915.     error = HGetVolParms((StringPtr)fileName, vRefNum, &volParmsInfo, &infoSize);
  1916.     if ( error == noErr )
  1917.     {
  1918.         /* if volume supports OpenRFDeny, use it and return */
  1919.         if ( hasOpenDeny(volParmsInfo) )
  1920.         {
  1921.             pb.accessParam.ioDenyModes = denyModes;
  1922.             error = PBHOpenRFDenySync(&pb);
  1923.             *refNum = pb.ioParam.ioRefNum;
  1924.             return ( error );
  1925.         }
  1926.     }
  1927.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  1928.         return ( error );
  1929.  
  1930.     /* OpenRFDeny isn't supported, so try File Manager OpenRF function */
  1931.     
  1932.     /* If request includes write permission, then see if the volume is */
  1933.     /* locked by hardware or software. The HFS file system doesn't check */
  1934.     /* for this when a file is opened - you only find out later when you */
  1935.     /* try to write and the write fails with a wPrErr or a vLckdErr. */
  1936.     
  1937.     if ( (denyModes & dmWr) != 0 )
  1938.     {
  1939.         error = CheckVolLock((StringPtr)fileName, vRefNum);
  1940.         if ( error != noErr )
  1941.             return ( error );
  1942.     }
  1943.     
  1944.     /* Set File Manager permissions to closest thing possible */
  1945.     pb.ioParam.ioPermssn = ((denyModes == dmWr) || (denyModes == dmRdWr)) ?
  1946.                            (fsRdWrShPerm) :
  1947.                            (denyModes % 4);
  1948.  
  1949.     error = PBHOpenRFSync(&pb);
  1950.     *refNum = pb.ioParam.ioRefNum;
  1951.     return ( error );
  1952. }
  1953.  
  1954. /*****************************************************************************/
  1955.  
  1956. pascal    OSErr    FSpOpenRFAware(const FSSpec *spec,
  1957.                                short denyModes,
  1958.                                short *refNum)
  1959. {
  1960.     return ( HOpenRFAware(spec->vRefNum, spec->parID, spec->name, denyModes, refNum) );
  1961. }
  1962.  
  1963. /*****************************************************************************/
  1964.  
  1965. pascal    OSErr    FSReadNoCache(short refNum,
  1966.                               long *count,
  1967.                               void *buffPtr)
  1968. {
  1969.     ParamBlockRec pb;
  1970.     OSErr error;
  1971.  
  1972.     pb.ioParam.ioRefNum = refNum;
  1973.     pb.ioParam.ioBuffer = (Ptr)buffPtr;
  1974.     pb.ioParam.ioReqCount = *count;
  1975.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;    /* fsAtMark + noCacheBit */
  1976.     pb.ioParam.ioPosOffset = 0;
  1977.     error = PBReadSync(&pb);
  1978.     *count = pb.ioParam.ioActCount;
  1979.     return ( error );
  1980. }
  1981.  
  1982. /*****************************************************************************/
  1983.  
  1984. pascal    OSErr    FSWriteNoCache(short refNum,
  1985.                                long *count,
  1986.                                const void *buffPtr)
  1987. {
  1988.     ParamBlockRec pb;
  1989.     OSErr error;
  1990.  
  1991.     pb.ioParam.ioRefNum = refNum;
  1992.     pb.ioParam.ioBuffer = (Ptr)buffPtr;
  1993.     pb.ioParam.ioReqCount = *count;
  1994.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;    /* fsAtMark + noCacheBit */
  1995.     pb.ioParam.ioPosOffset = 0;
  1996.     error = PBWriteSync(&pb);
  1997.     *count = pb.ioParam.ioActCount;
  1998.     return ( error );
  1999. }
  2000.  
  2001. /*****************************************************************************/
  2002.  
  2003. /*
  2004. **    See if numBytes bytes of buffer1 are equal to buffer2.
  2005. */
  2006. static    Boolean EqualMemory(const void *buffer1, const void *buffer2, unsigned long numBytes)
  2007. {
  2008.     register unsigned char *b1 = (unsigned char *)buffer1;
  2009.     register unsigned char *b2 = (unsigned char *)buffer2;
  2010.  
  2011.     if (b1 != b2)    /* if buffer pointers are same, then they are equal */
  2012.     {
  2013.         while ( numBytes > 0 )
  2014.         {
  2015.             /* compare the bytes and then increment the pointers */
  2016.             if ( (*b1++ - *b2++) != 0 )
  2017.             {
  2018.                 return ( false );
  2019.             }
  2020.             --numBytes;
  2021.         }
  2022.     }
  2023.     return ( true );
  2024. }
  2025.  
  2026. /*****************************************************************************/
  2027.  
  2028. /*
  2029. **    Read any number of bytes from an open file using read-verify mode.
  2030. **    The FSReadVerify function reads any number of bytes from an open file
  2031. **    and verifies them against the data in the buffer pointed to by buffPtr.
  2032. **    
  2033. **    Because of a bug in the HFS file system, only non-block aligned parts of
  2034. **    the read are verified against the buffer data and the rest is *copied*
  2035. **    into the buffer.  Thus, you shouldn't verify against your original data;
  2036. **    instead, you should verify against a copy of the original data and then
  2037. **    compare the read-verified copy against the original data after calling
  2038. **    FSReadVerify. That's why this function isn't exported - it needs the
  2039. **    wrapper provided by FSWriteVerify.
  2040. */
  2041. static    OSErr    FSReadVerify(short refNum,
  2042.                              long *count,
  2043.                              void *buffPtr)
  2044. {
  2045.     ParamBlockRec    pb;
  2046.     OSErr            result;
  2047.  
  2048.     pb.ioParam.ioRefNum = refNum;
  2049.     pb.ioParam.ioBuffer = (Ptr)buffPtr;
  2050.     pb.ioParam.ioReqCount = *count;
  2051.     pb.ioParam.ioPosMode = fsAtMark + rdVerify;
  2052.     pb.ioParam.ioPosOffset = 0;
  2053.     result = PBReadSync(&pb);
  2054.     *count = pb.ioParam.ioActCount;
  2055.     return ( result );
  2056. }
  2057.  
  2058. /*****************************************************************************/
  2059.  
  2060. pascal    OSErr    FSWriteVerify(short refNum,
  2061.                               long *count,
  2062.                               const void *buffPtr)
  2063. {
  2064.     Ptr        verifyBuffer;
  2065.     long    position;
  2066.     long    bufferSize;
  2067.     long    byteCount;
  2068.     long    bytesVerified;
  2069.     Ptr        startVerify;
  2070.     OSErr    result;
  2071.     
  2072.     /*
  2073.     **    Allocate the verify buffer
  2074.     **    Try to get get a large enough buffer to verify in one pass.
  2075.     **    If that fails, use GetTempBuffer to get a buffer.
  2076.     */
  2077.     bufferSize = *count;
  2078.     verifyBuffer = NewPtr(bufferSize);
  2079.     if ( verifyBuffer == NULL )
  2080.     {
  2081.         verifyBuffer = GetTempBuffer(bufferSize, &bufferSize);
  2082.     }
  2083.     if ( verifyBuffer != NULL )
  2084.     {        
  2085.         /* Save the current position */
  2086.         result = GetFPos(refNum, &position);
  2087.         if ( result == noErr )
  2088.         {
  2089.             /* Write the data */
  2090.             result = FSWrite(refNum, count, buffPtr);
  2091.             if ( result == noErr )
  2092.             {
  2093.                 /* Restore the original position */
  2094.                 result = SetFPos(refNum, fsFromStart, position);
  2095.                 if ( result == noErr )
  2096.                 {
  2097.                     /*
  2098.                     **    *count            = total number of bytes to verify
  2099.                     **    bufferSize        = the size of the verify buffer
  2100.                     **    bytesVerified    = number of bytes verified
  2101.                     **    byteCount        = number of bytes to verify this pass
  2102.                     **    startVerify        = position in buffPtr
  2103.                     */
  2104.                     bytesVerified = 0;
  2105.                     startVerify = (Ptr)buffPtr;
  2106.                     while ( (bytesVerified < *count) && ( result == noErr ) )
  2107.                     {
  2108.                         byteCount = ((*count - bytesVerified) > bufferSize) ?
  2109.                                     (bufferSize) :
  2110.                                     (*count - bytesVerified);
  2111.                         /*
  2112.                         **    Copy the write buffer into the verify buffer.
  2113.                         **    This step is needed because the File Manager
  2114.                         **    compares the data in any non-block aligned
  2115.                         **    data at the beginning and end of the read-verify
  2116.                         **    request back into the file system's cache
  2117.                         **    to the data in verify Buffer. However, the
  2118.                         **    File Manager does not compare any full blocks
  2119.                         **    and instead copies them into the verify buffer
  2120.                         **    so we still have to compare the buffers again
  2121.                         **    after the read-verify request completes.
  2122.                         */
  2123.                         BlockMoveData(startVerify, verifyBuffer, byteCount);
  2124.                         
  2125.                         /* Read-verify the data back into the verify buffer */
  2126.                         result = FSReadVerify(refNum, &byteCount, verifyBuffer);
  2127.                         if ( result == noErr )
  2128.                         {
  2129.                             /* See if the buffers are the same */
  2130.                             if ( !EqualMemory(verifyBuffer, startVerify, byteCount) )
  2131.                             {
  2132.                                 result = ioErr;
  2133.                             }
  2134.                             startVerify += byteCount;
  2135.                             bytesVerified += byteCount;
  2136.                         }
  2137.                     }
  2138.                 }
  2139.             }
  2140.         }
  2141.         DisposePtr(verifyBuffer);
  2142.     }
  2143.     else
  2144.     {
  2145.         result = memFullErr;
  2146.     }
  2147.     return ( result );
  2148. }
  2149.  
  2150. /*****************************************************************************/
  2151.  
  2152. pascal    OSErr    CopyFork(short srcRefNum,
  2153.                          short dstRefNum,
  2154.                          void *copyBufferPtr,
  2155.                          long copyBufferSize)
  2156. {
  2157.     ParamBlockRec srcPB;
  2158.     ParamBlockRec dstPB;
  2159.     OSErr srcError;
  2160.     OSErr dstError;
  2161.  
  2162.     if ( (copyBufferPtr == NULL) || (copyBufferSize == 0) )
  2163.         return ( paramErr );
  2164.     
  2165.     srcPB.ioParam.ioRefNum = srcRefNum;
  2166.     dstPB.ioParam.ioRefNum = dstRefNum;
  2167.  
  2168.     /* preallocate the destination fork and */
  2169.     /* ensure the destination fork's EOF is correct after the copy */
  2170.     srcError = PBGetEOFSync(&srcPB);
  2171.     if ( srcError != noErr )
  2172.         return ( srcError );
  2173.     dstPB.ioParam.ioMisc = srcPB.ioParam.ioMisc;
  2174.     dstError = PBSetEOFSync(&dstPB);
  2175.     if ( dstError != noErr )
  2176.         return ( dstError );
  2177.  
  2178.     /* reset source fork's mark */
  2179.     srcPB.ioParam.ioPosMode = fsFromStart;
  2180.     srcPB.ioParam.ioPosOffset = 0;
  2181.     srcError = PBSetFPosSync(&srcPB);
  2182.     if ( srcError != noErr )
  2183.         return ( srcError );
  2184.  
  2185.     /* reset destination fork's mark */
  2186.     dstPB.ioParam.ioPosMode = fsFromStart;
  2187.     dstPB.ioParam.ioPosOffset = 0;
  2188.     dstError = PBSetFPosSync(&dstPB);
  2189.     if ( dstError != noErr )
  2190.         return ( dstError );
  2191.  
  2192.     /* set up fields that won't change in the loop */
  2193.     srcPB.ioParam.ioBuffer = (Ptr)copyBufferPtr;
  2194.     srcPB.ioParam.ioPosMode = fsAtMark + 0x0020;/* fsAtMark + noCacheBit */
  2195.     srcPB.ioParam.ioReqCount = ((copyBufferSize >= 512) && (copyBufferSize % 512)) ?
  2196.                                (copyBufferSize / 512) * 512 :
  2197.                                (copyBufferSize);
  2198.         /* If copyBufferSize is greater than 512 bytes, make it a multiple of 512 bytes */
  2199.         /* This will make writes on local volumes faster */
  2200.  
  2201.     dstPB.ioParam.ioBuffer = (Ptr)copyBufferPtr;
  2202.     dstPB.ioParam.ioPosMode = fsAtMark + 0x0020;/* fsAtMark + noCacheBit */
  2203.  
  2204.     while ( (srcError == noErr) && (dstError == noErr) )
  2205.     {
  2206.         srcError = PBReadSync(&srcPB);
  2207.         dstPB.ioParam.ioReqCount = srcPB.ioParam.ioActCount;
  2208.         dstError = PBWriteSync(&dstPB);
  2209.     }
  2210.  
  2211.     /* make sure there were no errors at the destination */
  2212.     if ( dstError != noErr )
  2213.         return ( dstError );
  2214.  
  2215.     /* make sure the only error at the source was eofErr */
  2216.     if ( srcError != eofErr )
  2217.         return ( srcError );
  2218.  
  2219.     return ( noErr );
  2220. }
  2221.  
  2222. /*****************************************************************************/
  2223.  
  2224. pascal    OSErr    GetFileLocation(short refNum,
  2225.                                 short *vRefNum,
  2226.                                 long *dirID,
  2227.                                 StringPtr fileName)
  2228. {
  2229.     FCBPBRec pb;
  2230.     OSErr error;
  2231.  
  2232.     pb.ioNamePtr = fileName;
  2233.     pb.ioVRefNum = 0;
  2234.     pb.ioRefNum = refNum;
  2235.     pb.ioFCBIndx = 0;
  2236.     error = PBGetFCBInfoSync(&pb);
  2237.     *vRefNum = pb.ioFCBVRefNum;
  2238.     *dirID = pb.ioFCBParID;
  2239.     return ( error );
  2240. }
  2241.  
  2242. /*****************************************************************************/
  2243.  
  2244. pascal    OSErr    FSpGetFileLocation(short refNum,
  2245.                                    FSSpec *spec)
  2246. {
  2247.     return ( GetFileLocation(refNum, &(spec->vRefNum), &(spec->parID), spec->name) );
  2248. }
  2249.  
  2250. /*****************************************************************************/
  2251.  
  2252. pascal    OSErr    CopyDirectoryAccess(short srcVRefNum,
  2253.                                     long srcDirID,
  2254.                                     StringPtr srcName,
  2255.                                     short dstVRefNum,
  2256.                                     long dstDirID,
  2257.                                     StringPtr dstName)
  2258. {    
  2259.     OSErr err;
  2260.     GetVolParmsInfoBuffer infoBuffer;    /* Where PBGetVolParms dumps its info */
  2261.     long    dstServerAdr;                /* AppleTalk server address of destination (if any) */
  2262.     Boolean    dstHasBlankAccessPrivileges;
  2263.     long    ownerID, groupID, accessRights;
  2264.     long    tempLong;
  2265.  
  2266.     /* See if destination supports directory access control */
  2267.     tempLong = sizeof(infoBuffer);
  2268.     err = HGetVolParms(dstName, dstVRefNum, &infoBuffer, &tempLong);
  2269.     if ( (err != noErr) && (err != paramErr) )
  2270.         return ( err );
  2271.     if ( (err != paramErr) && hasAccessCntl(infoBuffer) )
  2272.     {
  2273.         dstServerAdr = infoBuffer.vMServerAdr;
  2274.         dstHasBlankAccessPrivileges = hasBlankAccessPrivileges(infoBuffer);
  2275.     }
  2276.     else
  2277.         /* If destination doesn't support access privileges, */
  2278.         /* then there's nothing left to do here */
  2279.         return ( noErr );
  2280.  
  2281.     /* We may have to do something with access privileges at the destination. */
  2282.     
  2283.     accessRights = 0;    /* clear so we can tell if anything was done with this */
  2284.  
  2285.     /* See if source supports directory access control and is on same server */
  2286.     tempLong = sizeof(infoBuffer);
  2287.     err = HGetVolParms(srcName, srcVRefNum, &infoBuffer, &tempLong);
  2288.     if ( (err != noErr) && (err != paramErr) )
  2289.         return (err);
  2290.     if ( (err != paramErr) && hasAccessCntl(infoBuffer) )
  2291.     {
  2292.         /* Make sure both locations are on the same file server */
  2293.         if ( dstServerAdr == infoBuffer.vMServerAdr )
  2294.         {
  2295.             /* copy'm */
  2296.             err = HGetDirAccess(srcVRefNum, srcDirID, srcName, &ownerID, &groupID, &accessRights);
  2297.             if ( err == noErr )
  2298.                 err = HSetDirAccess(dstVRefNum, dstDirID, dstName, ownerID, groupID, accessRights);
  2299.         }
  2300.     }
  2301.     return ( err );
  2302. }
  2303.  
  2304. /*****************************************************************************/
  2305.  
  2306. pascal    OSErr    FSpCopyDirectoryAccess(const FSSpec *srcSpec,
  2307.                                        const FSSpec *dstSpec)
  2308. {
  2309.     return ( CopyDirectoryAccess(srcSpec->vRefNum, srcSpec->parID, (StringPtr)srcSpec->name,
  2310.                                 dstSpec->vRefNum, dstSpec->parID, (StringPtr)dstSpec->name) );
  2311. }
  2312.  
  2313. /*****************************************************************************/
  2314.  
  2315. pascal    OSErr    HMoveRenameCompat(short vRefNum,
  2316.                                   long srcDirID,
  2317.                                   ConstStr255Param srcName,
  2318.                                   long dstDirID,
  2319.                                   StringPtr dstpathName,
  2320.                                   StringPtr copyName)
  2321. {
  2322.     OSErr                    error;
  2323.     GetVolParmsInfoBuffer    volParmsInfo;
  2324.     long                    infoSize = sizeof(GetVolParmsInfoBuffer);
  2325.     short                    realVRefNum;
  2326.     long                    realParID;
  2327.     Str255                    realName;
  2328.     short                    tempItemsVRefNum;
  2329.     long                    tempItemsDirID;
  2330.     Boolean                    isDirectory;
  2331.     
  2332.     /* get volume attributes */
  2333.     error = HGetVolParms((StringPtr)srcName, vRefNum, &volParmsInfo, &infoSize);
  2334.     if ( error == noErr )
  2335.     {
  2336.         /* if volume supports move and rename, use it and return */
  2337.         if ( hasMoveRename(volParmsInfo) )
  2338.             return (HMoveRename(vRefNum, srcDirID, srcName, dstDirID, dstpathName, copyName));
  2339.     }
  2340.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  2341.         return ( error );
  2342.     
  2343.     /* MoveRename isn't supported by this volume, so do it by hand */
  2344.     
  2345.     /* if copyName isn't supplied, we can simply CatMove and return */
  2346.     if ( copyName == NULL )
  2347.         return ( CatMove(vRefNum, srcDirID, srcName, dstDirID, dstpathName) );
  2348.     
  2349.     /* renaming is required, so we have some work to do... */
  2350.     
  2351.     /* get the object's real name */
  2352.     error = GetObjectLocation(vRefNum, srcDirID, (StringPtr)srcName,
  2353.                                 &realVRefNum, &realParID, realName, &isDirectory);
  2354.     if ( error != noErr )
  2355.         return ( error );
  2356.     
  2357.     /* find temporary items folder */
  2358.     error = FindFolder(realVRefNum, kTemporaryFolderType, kCreateFolder,
  2359.                         &tempItemsVRefNum, &tempItemsDirID);
  2360.     if ( error != noErr )
  2361.         return ( error );
  2362.     
  2363.     /* move the object to a temporary items folder for renaming */
  2364.     error = CatMove(realVRefNum, realParID, realName, tempItemsDirID, NULL);
  2365.     if ( error != noErr )
  2366.         return ( error );
  2367.     
  2368.     /* rename the object */    
  2369.     error = HRename(tempItemsVRefNum, tempItemsDirID, realName, copyName);
  2370.     if ( error == noErr )
  2371.     {
  2372.         /* move object to its new home */
  2373.         error = CatMove(tempItemsVRefNum, tempItemsDirID, copyName, dstDirID, dstpathName);
  2374.         if ( error == noErr )
  2375.             return ( error );    /* IF NO ERRORS EXIT HERE! */
  2376.         
  2377.         /* Error handling: rename object back to original name - keep real error */
  2378.         (void) HRename(tempItemsVRefNum, tempItemsDirID, copyName, realName);
  2379.     }
  2380.     
  2381.     /* Error handling: move object back to original location - keep real error */
  2382.     (void) CatMove(tempItemsVRefNum, tempItemsDirID, realName, realParID, NULL);
  2383.     
  2384.     return ( error );
  2385. }
  2386.  
  2387. /*****************************************************************************/
  2388.  
  2389. pascal    OSErr    FSpMoveRenameCompat(const FSSpec *srcSpec,
  2390.                                     const FSSpec *dstSpec,
  2391.                                     StringPtr copyName)
  2392. {
  2393.     /* make sure the FSSpecs refer to the same volume */
  2394.     if (srcSpec->vRefNum != dstSpec->vRefNum)
  2395.         return (diffVolErr);
  2396.     return ( HMoveRenameCompat(srcSpec->vRefNum, srcSpec->parID, srcSpec->name,
  2397.                       dstSpec->parID, (StringPtr)dstSpec->name, copyName) );
  2398. }
  2399.  
  2400. /*****************************************************************************/
  2401.  
  2402. pascal    void    BuildAFPVolMountInfo(short theFlags,
  2403.                                      char theNBPInterval,
  2404.                                      char theNBPCount,
  2405.                                      short theUAMType,
  2406.                                      Str31 theZoneName,
  2407.                                      Str31 theServerName,
  2408.                                      Str27 theVolName,
  2409.                                      Str31 theUserName,
  2410.                                      Str8 theUserPassWord,
  2411.                                      Str8 theVolPassWord,
  2412.                                      MyAFPVolMountInfoPtr theAFPInfo)
  2413. {
  2414.     /* Fill in an AFPVolMountInfo record that can be passed to VolumeMount */
  2415.     theAFPInfo->length = sizeof(MyAFPVolMountInfo);
  2416.     theAFPInfo->media = AppleShareMediaType;
  2417.     theAFPInfo->flags = theFlags;
  2418.     theAFPInfo->nbpInterval = theNBPInterval;
  2419.     theAFPInfo->nbpCount = theNBPCount;
  2420.     theAFPInfo->uamType = theUAMType;
  2421.     theAFPInfo->zoneNameOffset = (short)((long)theAFPInfo->zoneName - (long)theAFPInfo);
  2422.     theAFPInfo->serverNameOffset = (short)((long)theAFPInfo->serverName - (long)theAFPInfo);
  2423.     theAFPInfo->volNameOffset = (short)((long)theAFPInfo->volName - (long)theAFPInfo);
  2424.     theAFPInfo->userNameOffset = (short)((long)theAFPInfo->userName - (long)theAFPInfo);
  2425.     theAFPInfo->userPasswordOffset = (short)((long)theAFPInfo->userPassword - (long)theAFPInfo);
  2426.     theAFPInfo->volPasswordOffset = (short)((long)theAFPInfo->volPassword - (long)theAFPInfo);
  2427.     
  2428.     BlockMoveData(theZoneName, theAFPInfo->zoneName, theZoneName[0] + 1);
  2429.     BlockMoveData(theServerName, theAFPInfo->serverName, theServerName[0] + 1);
  2430.     BlockMoveData(theVolName, theAFPInfo->volName, theVolName[0] + 1);
  2431.     BlockMoveData(theUserName, theAFPInfo->userName, theUserName[0] + 1);
  2432.     BlockMoveData(theUserPassWord, theAFPInfo->userPassword, theUserPassWord[0] + 1);
  2433.     BlockMoveData(theVolPassWord, theAFPInfo->volPassword, theVolPassWord[0] + 1);
  2434. }
  2435.  
  2436. /*****************************************************************************/
  2437.  
  2438. pascal    OSErr    RetrieveAFPVolMountInfo(AFPVolMountInfoPtr theAFPInfo,
  2439.                                         short *theFlags,
  2440.                                         short *theUAMType,
  2441.                                         StringPtr theZoneName,
  2442.                                         StringPtr theServerName,
  2443.                                         StringPtr theVolName,
  2444.                                         StringPtr theUserName)
  2445. {
  2446.     OSErr        error;
  2447.     StringPtr    tempPtr;
  2448.         
  2449.     /* Retrieve the AFP mounting information from an AFPVolMountInfo record. */
  2450.     if ( theAFPInfo->media == AppleShareMediaType )
  2451.     {
  2452.         *theFlags = theAFPInfo->flags;
  2453.         *theUAMType = theAFPInfo->uamType;
  2454.         
  2455.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->zoneNameOffset);
  2456.         BlockMoveData(tempPtr, theZoneName, tempPtr[0] + 1);
  2457.         
  2458.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->serverNameOffset);
  2459.         BlockMoveData(tempPtr, theServerName, tempPtr[0] + 1);
  2460.         
  2461.         tempPtr = (StringPtr)(StringPtr)((long)theAFPInfo + theAFPInfo->volNameOffset);
  2462.         BlockMoveData(tempPtr, theVolName, tempPtr[0] + 1);
  2463.         
  2464.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->userNameOffset);
  2465.         BlockMoveData(tempPtr, theUserName, tempPtr[0] + 1);
  2466.         
  2467.         error = noErr;
  2468.     }
  2469.     else
  2470.     {
  2471.         error = paramErr;
  2472.     }
  2473.     
  2474.     return ( error );
  2475. }
  2476.  
  2477. /*****************************************************************************/
  2478.  
  2479. pascal    OSErr    GetUGEntries(short objType,
  2480.                              UGEntryPtr entries,
  2481.                              long reqEntryCount,
  2482.                              long *actEntryCount,
  2483.                              long *objID)
  2484. {
  2485.     HParamBlockRec pb;
  2486.     OSErr error = noErr;
  2487.     UGEntry *endEntryArray = entries + reqEntryCount;
  2488.  
  2489.     pb.objParam.ioObjType = objType;
  2490.     *actEntryCount = 0;
  2491.     for ( ; (entries < endEntryArray) && (error == noErr); ++entries )
  2492.     {
  2493.         pb.objParam.ioObjNamePtr = (StringPtr)entries->name;
  2494.         pb.objParam.ioObjID = *objID;
  2495.         /* Files.h in the universal interfaces, PBGetUGEntrySync takes a CMovePBPtr */
  2496.         /* as the parameter. Inside Macintosh and the original glue used HParmBlkPtr. */
  2497.         /* A CMovePBPtr works OK, but this will be changed in the future  back to */
  2498.         /* HParmBlkPtr, so I'm just casting it here. */
  2499.         error = PBGetUGEntrySync(&pb);
  2500.         if ( error == noErr )
  2501.         {
  2502.             entries->objID = *objID = pb.objParam.ioObjID;
  2503.             entries->objType = objType;
  2504.             ++*actEntryCount;
  2505.         }
  2506.     }
  2507.     return ( error );
  2508. }
  2509.